package top.maof.book.spider.develop.contents.boluo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import top.maof.book.model.Book;
import top.maof.book.model.Contents;
import top.maof.book.spider.develop.Split;
import top.maof.book.utils.URLUtils;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.model.AfterExtractor;
import us.codecraft.webmagic.model.annotation.ExtractBy;
import us.codecraft.webmagic.model.annotation.HelpUrl;
import us.codecraft.webmagic.model.annotation.TargetUrl;

import java.text.SimpleDateFormat;
import java.util.Date;

@HelpUrl(value = {"https://www.boluoxs.com/xiaoshuo/quanbu.html", "https://www.boluoxs.com/book/*"})
@TargetUrl("https://www.boluoxs.com/biquge/\\d+/\\d+")
@Data
@Slf4j
public class Boluoxs implements AfterExtractor, Split {
    @ExtractBy(value = "//div[@class=eg]/span[@class=egf]/h1/text()")
    private String name;

    @ExtractBy(value = "//div[@class=eg]/span[@class=egf]/h3/text()")
    private String author;

    @ExtractBy(value = "//div[@class=img]/img/@src")
    private String img;

    @ExtractBy(value = "//div[@id=maininfo]/div[@class=info]/p[2]/text()")
    private String classify;//所属类型：武侠修真

    //@ExtractBy(value = "//div[@id=maininfo]/div[@class=info]/p[@class=s]/text()")//更新时间：2019-07-10 08:51
    private Date updateDate;

    private Date insertDate = new Date();

    @ExtractBy(value = "//div[@id=intro]/p[1]/text()")
    private String synopsis;

    @ExtractBy(value = "//div[@id=maininfo]/div[@class=info]/p[3]/text()")//连载完成：连载
    private String state;

    private String url;

    //@ExtractBy("//div[@class=article]/div[@class=article_texttitleb]/div[@class=article_texttitleb]")
    private String content;

    private Integer webId = 3;

    public static final String CHARSET = "gb2312";


    @Override
    public void afterProcess(Page page) {
        //获取最新章节更新时间   最后更新：2019-04-17 02:11:06
        String updateStr = page.getHtml().xpath("//div[@id=maininfo]/div[@class=info]/p[@class=s]/text()").toString().substring(5, 16).trim();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            this.updateDate = simpleDateFormat.parse(updateStr);
            Date today = new Date();
            //如果最近更新时间和抓取当天时间相隔一个月,便默认更新完毕或已断更,保存html
            if ((today.getTime() - this.updateDate.getTime()) > 1000 * 60 * 60 * 24 * 30L) {
                // this.content = page.getHtml().xpath("//div[@class=article]/div[@class=article_texttitleb]/div[@class=article_texttitleb]").toString();
                this.state = "完本";
            } else {
                this.state = "连载";
            }
        } catch (Exception e) {
            log.info(e.getMessage());
        }

        //所属类型：武侠修真
        this.classify = this.classify.substring(5, this.classify.length());


        //img字段做处理
      /*  if (!this.img.startsWith("http")) {
            if (this.img.startsWith("/"))
                this.img = "https://www.boluoxs.org" + this.img;
            else {
                this.img = page.getUrl().toString().substring(0, page.getUrl().toString().lastIndexOf("/") + 1) + this.img;
            }
        }*/
        //获取该页面url
        this.url = page.getUrl().toString();

        try{
            this.img= URLUtils.completeUrl(this.url,this.img);
        }catch (Exception e){
            log.info(e.getMessage());
        }

        //对得到的作者字段做处理  作者：唐家三少
        this.author = this.author.substring(3, this.author.length());
    }


    @Override
    public Book getBook() {
        Book book = new Book();
        book.setAuthor(this.author);
        book.setState(this.state);
        book.setImg(this.img);
        book.setName(this.name);
        book.setInsertDate(this.insertDate);
        book.setClassify(this.classify);
        book.setSynopsis(this.synopsis);
        book.setUpdateDate(this.updateDate);
        return book;
    }

    @Override
    public Contents getContents() {
        Contents contents = new Contents();
        contents.setUrl(this.url);
        contents.setContent(this.content);
        contents.setWebId(this.webId);
        contents.setUpdateDate(this.updateDate);
        return contents;
    }

}

